package com.xmut.mall.coupon.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.coupon.dao.PageDataDao;
import com.xmut.mall.coupon.entity.PageDataEntity;
import com.xmut.mall.coupon.searchparams.SellerPageDataSearchParam;
import com.xmut.mall.coupon.service.PageDataService;
import com.xmut.mall.coupon.utils.PageUtil;
import com.xmut.mall.coupon.utils.RespBean;
import com.xmut.mall.coupon.vo.AddPageDataVo;
import com.xmut.mall.coupon.vo.PageVO;
import com.xmut.mall.coupon.vo.SaveFloorTemplateVo;
import com.xmut.mall.coupon.vo.SellerPageDataPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service("pageDataService")
public class PageDataServiceImpl extends ServiceImpl<PageDataDao, PageDataEntity> implements PageDataService {

    /**
     * 获取首页数据
     *
     * @return
     */
    @Cacheable(value = "indexDate", key = "#root.methodName")
    @Override
    public RespBean getIndexData() {
        // 这里写死 因为只有一种格式
        PageDataEntity data = this.getById(1353621558308659200L);
        return RespBean.success("获取首页数据成功", data.getPageData());
    }

    /**
     * 获取卖家端的店铺装修数据
     *
     * @param vo
     * @param param
     * @return
     */
    @Override
    public RespBean getSellerPageDataByPage(PageVO vo, SellerPageDataSearchParam param) {
        IPage<SellerPageDataPageVo> page = this.baseMapper.getSellerPageDataByPage(PageUtil.initPage(vo), param);

        return RespBean.success("获取成功", page);

    }

    /**
     * 添加装修数据
     *
     * @param vo
     * @return
     */
    @Override
    public RespBean addPageData(AddPageDataVo vo) {
        PageDataEntity entity = new PageDataEntity();

        BeanUtils.copyProperties(vo, entity);
        entity.setCreateTime(new Date());

        boolean save = this.save(entity);
        if (save) {
            return RespBean.success("添加成功");
        } else {
            return RespBean.error("添加失败");
        }

    }

    /**
     * 编辑页面装修数据的名称
     *
     * @param params
     * @return
     */
    @Override
    public RespBean editPageDataName(Map<String, Object> params) {
        Long id = MapUtil.getLong(params, "id");
        String name = MapUtil.getStr(params, "name");

        boolean update = this.update(new UpdateWrapper<PageDataEntity>().set("name", name).eq("id", id));

        if (update) {
            return RespBean.success("修改成功");
        } else {
            return RespBean.error("修改失败");
        }

    }

    /**
     * 删除装修数据
     *
     * @param id
     * @return
     */
    @Override
    public RespBean deletePageData(Long id) {
        boolean b = this.removeById(id);
        if (b) {
            return RespBean.success("删除成功");
        } else {
            return RespBean.error("删除失败");
        }

    }

    /**
     * 改变页面装修发布状态
     *
     * @param param
     * @return
     */
    @Override
    public RespBean updatePageDataShow(Map<String, Object> param) {
        Long id = MapUtil.getLong(param, "id");
        Long storeId = MapUtil.getLong(param, "storeId");
        Boolean pageShow = MapUtil.getBool(param, "pageShow");

        boolean flag = false;

        if (pageShow) {
            // 如果是开启 先把所有当前店铺的模板变为不开启
            this.update(new UpdateWrapper<PageDataEntity>().set("page_show", "CLOSE").eq("store_id", storeId));
            // 然后把当前要开启的模板变为可用
            flag = this.update(new UpdateWrapper<PageDataEntity>().set("page_show", "OPEN").eq("id", id));
        } else {
            // 如果是关闭 直接修改状态为False
            flag = this.update(new UpdateWrapper<PageDataEntity>().set("page_show", "CLOSE").eq("id", id));
        }

        if (flag) {
            return RespBean.success("修改成功");
        } else {
            return RespBean.error("修改失败");
        }
    }

    /**
     * 根据ID获取页面装修数据
     *
     * @param id
     * @return
     */
    @Override
    public RespBean getPageData(Long id) {
        PageDataEntity entity = this.getById(id);

        return RespBean.success("获取成功", entity);

    }

    /**
     * 保存用户添加的装修模板
     *
     * @param vo
     * @return
     */
    @Override
    public RespBean saveFloorTemplate(SaveFloorTemplateVo vo) {
        boolean update = this.update(new UpdateWrapper<PageDataEntity>()
                .set("page_data", vo.getPageData())
                .set("page_show", vo.getPageShow())
                .eq("id", vo.getId()));

        if (update) {
            return RespBean.success("保存成功");
        } else {
            return RespBean.error("保存失败");
        }

    }

    /**
     * 根据Map参数获取装修数据
     *
     * @param params
     * @return
     */
    @Override
    public RespBean getPageDataByMap(Map<String, Object> params) {
        String type = MapUtil.getStr(params, "type"); // 页面的类型
        Long storeId = MapUtil.getLong(params, "storeId");
        String clientType = MapUtil.getStr(params, "clientType");// 客户端类型

        PageDataEntity data = this.getOne(new QueryWrapper<PageDataEntity>()
                .eq("page_client_type", clientType)
                .eq("page_show", "OPEN")
                .eq("page_type", type)
                .eq("store_id", storeId));

        return RespBean.success("获取成功", data);
    }


}
